﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    public partial class BudgetProgress : Form
    {
        //ProgressBar progressBar1;
        SmoothProgressBar progressBar1;
        System.Windows.Forms.Label txtCategory;
        System.Windows.Forms.Label txtDesc;
        System.Windows.Forms.PictureBox picBox;
        List<Bitmap> listImages = new List<Bitmap>();
        List<Rectangle> listRectangles = new List<Rectangle>();
        List<Point> listPoints = new List<Point>();
        private int rectangles_drawn;
        Bitmap image1;
        Rectangle R;
        private int yStart = 51;
        private int xProgBarLoc = 100;
        Dictionary<string, decimal> d = new Dictionary<string, decimal>();
        Color green = Color.FromArgb(0, 211, 40);
        Color red = Color.FromArgb(225,76,55);
        private DateTime datePicked;
        private DateTime AssociatedFriday;

        public Dictionary<string, ProgressBar> dictBudgetProgressBars = new Dictionary<string, ProgressBar>();
        public BudgetProgress()
        {
            InitializeComponent();
            rectangles_drawn = 0;
            //for testing
            //dateTimePicker1.Value = new DateTime(2011, 2, 27);


            datePicked = dateTimePicker1.Value;
            AssociatedFriday = Dates.getFridayAssociatedWithDate(datePicked);
            
            GetExpenses();

            int daysInMonth = Dates.getWeeksInMonth(AssociatedFriday.Month, AssociatedFriday.Year) * 7;
            int percentDaysIntoMonth = Convert.ToInt32((Dates.GetDayOfMonth(datePicked)*100) / daysInMonth);
                //Convert.ToInt32((Dates.GetDayOfMonth(dateTimePicker1.Value) * 100) / daysInMonth);

            foreach(string s in dbData.listExpenseCategories)
            {
                this.txtCategory = new Label();
                this.txtCategory.Location = new System.Drawing.Point(xProgBarLoc - 85, yStart+5);
                this.txtCategory.Name = "lblCategory"+ rectangles_drawn;
                this.txtCategory.Text = s;
                this.Controls.Add(this.txtCategory);

                this.txtDesc = new Label();
                this.txtDesc.Location = new System.Drawing.Point(xProgBarLoc + 734 + 5, yStart + 5);
                this.txtDesc.Name = "txtDesc" + rectangles_drawn;
                this.txtDesc.Text = String.Format("{0}/{1}", d[s].ToString(Money.MoneyFormat), dbData.CategoryBudgets[s].ToString(Money.MoneyFormat));
                this.Controls.Add(this.txtDesc);

                this.progressBar1 = new SmoothProgressBar();
                this.progressBar1.Location = new System.Drawing.Point(xProgBarLoc, yStart);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(734, 23);
                this.progressBar1.TabIndex = 0;
                this.progressBar1.Minimum = 0;
                this.progressBar1.Maximum = Convert.ToInt32(dbData.CategoryBudgets[s] * 100);

                //if the expense is fixed
                if(dbData.CategoryFixed[s] == 1)
                {
                    //max = 100, we will calculate it as a percent
                    this.progressBar1.Maximum = 100;
                    //value is the number of days that have passed divided by total days in month * 100
                    this.progressBar1.Value = percentDaysIntoMonth;
                    
                    this.progressBar1.ProgressBarColor = green;
                }
                else if (progressBar1.Maximum > 0 &&
                Convert.ToInt32(d[s] * 100) <= progressBar1.Maximum &&
                100 * Convert.ToInt32(d[s] * 100) / this.progressBar1.Maximum > percentDaysIntoMonth)
                {
                    this.progressBar1.ProgressBarColor = Color.Yellow;
                    this.progressBar1.Value = Convert.ToInt32(d[s] * 100);
                }
                //if we are over the budget for the month
                else if (this.progressBar1.Maximum < Convert.ToInt32(d[s] * 100))
                {
                    this.progressBar1.Value = this.progressBar1.Maximum;
                    //set color to red
                    this.progressBar1.ProgressBarColor = red;
                }
                else
                {
                    //we aren't over budget
                    this.progressBar1.Value = Convert.ToInt32(d[s] * 100);
                    this.progressBar1.ProgressBarColor = green;
                }

                //if we have a budget set max > 0
                //if we aren't over budget val < max
                //if we the value as a percent is greater than the percentage of days that have elapsed this month
                //we are over so far, but not screwed yet
                

                if(dbData.CategoryBudgets[s] == 0 && d[s] > 0 && dbData.CategoryFixed[s] == 0)
                {
                    this.progressBar1.Maximum = 1;
                    this.progressBar1.Value = this.progressBar1.Maximum;
                    //set color to red
                    this.progressBar1.ProgressBarColor = red;
                }

                this.progressBar1.BackColor = Color.LightGray;
                this.Controls.Add(this.progressBar1);

                if (Dates.getFridayAssociatedWithDate(datePicked).Month == Dates.getFridayAssociatedWithDate(DateTime.Now).Month)
                {
                    image1 = new Bitmap("bar.bmp");
                    listImages.Add(image1);
                    this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);

                    listPoints.Add(new Point(xProgBarLoc + (723 *
                                   Dates.GetDayOfMonth(DateTime.Now) / daysInMonth)
                        ,
                        yStart - 2));

                    //R = new Rectangle(listPoints[listPoints.Count-1], listImages[listImages.Count-1].Size);
                    //listRectangles.Add(R);
                    //this.Invalidate(listRectangles[listRectangles.Count-1]);
                    picBox = new PictureBox();
                    picBox.Image = image1;
                    picBox.Location = listPoints[listPoints.Count - 1];
                    picBox.Size = image1.Size;
                    this.Controls.Add(picBox);
                }

                yStart += 30;
                progressBar1.SendToBack();
                txtCategory.SendToBack();
            }
           
            this.ResumeLayout(false);
        }
        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (listImages.Count > rectangles_drawn &&
                listImages[rectangles_drawn] != null )
            {
                e.Graphics.DrawImage(listImages[rectangles_drawn], listPoints[rectangles_drawn]);
                rectangles_drawn++;
            }
        }
        private void GetExpenses()
        {
            DataTable dt_expenses;
            //int weeksInMonth = Dates.getWeeksInMonth(dateTimePicker1.Value.Month, dateTimePicker1.Value.Year);
            DateTime start = Dates.getFirstTransactionDayOfMonth(AssociatedFriday);
                //Dates.getFirstFridayInMonth(dateTimePicker1.Value.Month, dateTimePicker1.Value.Year).AddDays(-6);
            DateTime end = Dates.getLastTransactionDayofMonth(AssociatedFriday);
                //start.AddDays(Convert.ToDouble(weeksInMonth*7));
            dt_expenses = DatabaseQuery.ExecStringProc(
                        StoredProcs.getExpenseForWeek(
                            start.ToString("MM/dd/yyyy"),
                            end.ToString("MM/dd/yyyy"),
                            ""));

            
            foreach (string s in dbData.listExpenseCategories)
            {
                d.Add(s, 0);
            }
            foreach(DataRow dr in dt_expenses.Rows)
            {
                d[dr["tblCategoryExpense.Category"].ToString()] += Convert.ToDecimal(dr["Amount"]);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            //this.d
        }



    }
}
